package com.opencloud.iot.provider.controller;

import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.ResultBody;
import com.opencloud.iot.client.model.entity.IotRaspiBind;
import com.opencloud.iot.client.model.entity.IotRaspiBindInfo;
import com.opencloud.iot.client.model.entity.IotRaspiInfo;
import com.opencloud.iot.provider.service.IotWatchinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 物联设备（树莓派）信息
 * @author yeyong
 */
@RestController
@Api(value = "物联网设备", tags = "物联网设备-树莓派信息")
public class IotRaspiController {
    @Autowired
    private IotWatchinfoService iotWatchinfoService;


    /**
     * 新增树莓派信息
     * @param mac
     * @param localip
     * @param devicesecret
     * @param productkey
     * @param devicename
     * @param s_ver
     * @param h_ver
     * @param vnc_port
     * @param ssh_port
     * @return
     */
    @ApiOperation(value = "新增树莓派信息", notes = "新增树莓派信息")
    @PostMapping("/iot/addraspiinfo")
    public ResultBody<IotRaspiInfo> addIotRaspiInfo
    (
            @RequestParam(name = "mac",required = true) String mac,
            @RequestParam(name = "localip",required = true) String localip,
            @RequestParam(name = "devicesecret",required = true) String devicesecret,
            @RequestParam(name = "productkey",required = true) String productkey,
            @RequestParam(name = "devicename",required = true) String  devicename,
            @RequestParam(name = "s_ver",required = true) String  s_ver,
            @RequestParam(name = "h_ver",required = true) String  h_ver,
            @RequestParam(name = "vnc_port",required = true) Integer  vnc_port,
            @RequestParam(name = "ssh_port",required = true) Integer  ssh_port
    )
    {
        IotRaspiInfo entiy = new IotRaspiInfo();
        entiy.setDevicename(devicename);
        entiy.setDevicesecret(devicesecret);
        entiy.setLocalip(localip);
        entiy.setMac(mac);
        entiy.setProductkey(productkey);
        entiy.setsVer(s_ver);
        entiy.sethVer(h_ver);
        entiy.setVncPort(vnc_port);
        entiy.setSshPort(ssh_port);
        entiy.setCreatetime(new Date());
        entiy.setUpdatetime(new Date());
        entiy = iotWatchinfoService.addIotRaspiInfo(entiy);
        return ResultBody.ok().data(entiy);
    }

    /**
     * 更新树莓派信息
     * @param id
     * @param localip
     * @param devicesecret
     * @param productkey
     * @param devicename
     * @param s_ver
     * @param h_ver
     * @param vnc_port
     * @param ssh_port
     * @return
     */
    @ApiOperation(value = "更新树莓派信息", notes = "更新树莓派信息")
    @PostMapping("/iot/updateraspiinfo")
    public ResultBody<IotRaspiInfo> updateIotRaspiInfo
    (
            @RequestParam(name = "id",required = true) Integer id,
            @RequestParam(name = "localip",required = true) String localip,
            @RequestParam(name = "devicesecret",required = true) String devicesecret,
            @RequestParam(name = "productkey",required = true) String productkey,
            @RequestParam(name = "devicename",required = true) String  devicename,
            @RequestParam(name = "s_ver",required = true) String  s_ver,
            @RequestParam(name = "h_ver",required = true) String  h_ver,
            @RequestParam(name = "vnc_port",required = true) Integer  vnc_port,
            @RequestParam(name = "ssh_port",required = true) Integer  ssh_port
    )
    {
        IotRaspiInfo entiy = new IotRaspiInfo();
        entiy = iotWatchinfoService.getIotRaspiInfoById(id);
        if(entiy!=null) {
            entiy.setDevicename(devicename);
            entiy.setDevicesecret(devicesecret);
            entiy.setLocalip(localip);
            entiy.setProductkey(productkey);
            entiy.setsVer(s_ver);
            entiy.sethVer(h_ver);
            entiy.setVncPort(vnc_port);
            entiy.setSshPort(ssh_port);
            entiy.setUpdatetime(new Date());
            entiy = iotWatchinfoService.updateIotRaspiInfo(entiy);
            return ResultBody.ok().data(entiy);
        }else{
            return ResultBody.failed().data(-1);
        }
    }

    /**
     * 更新树莓派状态
     * @param id
     * @param online_status
     * @param status
     * @return
     */
    @ApiOperation(value = "更新树莓派状态", notes = "更新树莓派状态")
    @PostMapping("/iot/updateraspistatus")
    public ResultBody<IotRaspiInfo> updateIotRaspiStatus
    (
            @RequestParam(name = "id",required = true) Integer id,
            @RequestParam(name = "online_status",required = true) Integer  online_status,
            @RequestParam(name = "status",required = true) Integer  status
    )
    {
        IotRaspiInfo entiy = new IotRaspiInfo();
        entiy = iotWatchinfoService.getIotRaspiInfoById(id);
        if(entiy!=null) {
            entiy.setOnlineStatus(online_status);
            entiy.setStatus(status);
            entiy.setUpdatetime(new Date());
            entiy = iotWatchinfoService.updateIotRaspiInfo(entiy);
            return ResultBody.ok().data(entiy);
        }else{
            return ResultBody.failed().data(-1);
        }
    }

    /**
     * 新增树莓派绑定
     * @param mac
     * @param cid
     * @return
     */
    @ApiOperation(value = "新增树莓派绑定", notes = "新增树莓派绑定")
    @PostMapping("/iot/addraspibind")
    public ResultBody<IotRaspiBind> addIotRaspiBind
    (
            @RequestParam(name = "mac",required = true) String mac,
            @RequestParam(name = "cid",required = true) Integer cid,
            @RequestParam(name = "cname",required = true) String cname,
            @RequestParam(name = "gid",required = true) Integer gid,
            @RequestParam(name = "gname",required = true) String gname
    )
    {
        IotRaspiBind entiy = new IotRaspiBind();
        entiy.setCid(cid);
        entiy.setMac(mac);
        entiy.setCname(cname);
        entiy.setGid(gid);
        entiy.setGname(gname);
        entiy.setCtime(new Date());
        entiy = iotWatchinfoService.addIotRaspiBind(entiy);
        return ResultBody.ok().data(entiy);
    }

    /**
     * 解除树莓派绑定
     * @param mac
     * @param cid
     * @return
     */
    @ApiOperation(value = "解除树莓派绑定", notes = "解除树莓派绑定")
    @PostMapping("/bdb/deleteraspibind")
    public ResultBody<Integer> deleteIotRaspiBind
    (
            @RequestParam(name = "mac",required = true) String mac,
            @RequestParam(name = "cid",required = true) Integer cid
    )
    {
        IotRaspiBind entiy = new IotRaspiBind();
        entiy.setCid(cid);
        entiy.setMac(mac);
        iotWatchinfoService.deleteIotRaspiBind(entiy);
        return ResultBody.ok().data(1);
    }

    @ApiOperation(value = "获取树莓派列表", notes = "获取树莓派列表")
    @GetMapping("/iot/bflraspilist")
    public ResultBody<List<IotRaspiInfo>> getIotRaspiList(
            @RequestParam(required = false) Map map
    )
    {
        List<IotRaspiInfo> lt = new ArrayList<>();
        lt = iotWatchinfoService.findIotRaspiInfoList(new PageParams(map));
        return ResultBody.ok().data(lt);
    }

    /**
     * 获取班级绑定的树莓派列表
     * @param cid
     * @return
     */
    @ApiOperation(value = "获取班级绑定的树莓派列表", notes = "获取班级绑定的树莓派列表")
    @GetMapping("/iot/bflraspilisbyclass")
    public ResultBody<List<IotRaspiInfo>> getIotRaspiList(
            @RequestParam(required = false) Integer cid
    )
    {
        List<IotRaspiBind> lt = new ArrayList<>();
        List<IotRaspiInfo> raspiInfos = new ArrayList<>();
        lt = iotWatchinfoService.getIotRaspiBindByClassId(cid);
        for(IotRaspiBind entiy:lt){
            IotRaspiInfo raspiInfo = iotWatchinfoService.getIotRaspiInfoByMac(entiy.getMac());
            raspiInfos.add(raspiInfo);
        }
        return ResultBody.ok().data(raspiInfos);
    }

    @ApiOperation(value = "获取树莓派列表以及绑定情况", notes = "获取树莓派列表以及绑定情况")
    @GetMapping("/iot/bflraspialllist")
    public ResultBody<List<IotRaspiBindInfo>> getIotRaspiBindInfoList(
            @RequestParam(required = false) String mac,
            @RequestParam(required = false) Integer gid,
            @RequestParam(required = false) Integer classid
    )
    {
        List<IotRaspiBindInfo> lt = new ArrayList<>();
        lt = iotWatchinfoService.getIotRaspiBindList(mac,gid,classid);
        return ResultBody.ok().data(lt);
    }
}
